/*
	The MIT License

	Copyright (c) 2010 IFMO/GameDev Studio

	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in
	all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
	THE SOFTWARE.
*/


#include "d3d_local.h"
#include <algorithm>

IDSScene	*CreateDSScene	( void ) { return ERScene(); }

/*-----------------------------------------------------------------------------
	Scene stuff :
-----------------------------------------------------------------------------*/


//
//	ERScene::ERScene
//
ERScene::ERScene( ED3DRenderSystem *rs )
{
	LOG_INIT("Scene");

	//
	//	Check device :
	//	
	if (!rs->CheckFallback(RS_FEATURE_RAWZ) ) {
		FATAL("Readable Z-buffer is not supported");
	}
	
	CONFIG_REGISTER_VAR(rs_wireframe, false);

	//
	//	Init :
	//	
	this->rs	=	rs;
	rtm			=	rs->rtm;
	d3ddev		=	rs->GetDevice();
	
	draw2d	=	rs->GetDraw2D();
	
	sshs		=	new ERSShadingSystem(this);
	light_rend	=	new ERSLightRenderer(this);
	scr_fx		=	new ERSScreenFX(this);
	gfx			=	new ERSGFXSystem(this);

	LuaDefineCFunction(CoreLua(), "reloadShaders", ReloadShaders, this);
}


//
//	ERScene::~ERScene
//
ERScene::~ERScene( void )
{
	sshs		=	NULL;
	light_rend	=	NULL;
	scr_fx		=	NULL;
	gfx			=	NULL;
	LOG_SHUTDOWN("Scene");
}


/*-----------------------------------------------------------------------------
	Scene management :
-----------------------------------------------------------------------------*/

//
//	ERScene::AddLight
//
IPxRSLight ERScene::AddLight( const ERendLightDesc_s *light_desc )
{
	IPxRSLight	light = new ERSLight(this, light_desc);

	lights.push_back( light );	
	
	return light;
}


//
//	ERScene::AddEntity
//
IPxRSEntity ERScene::AddEntity( IPxTriMesh mesh, const EVec4 &pos, const EQuat &orient )
{
	IPxRSEntity	ent = new ERSEntity(this, mesh, pos, orient);
	
	entities.push_back(ent);
	
	return ent;
}


//
//	ERScene::RemoveLight
//
void ERScene::RemoveLight( IPxRSLight light )
{
	vector<IPxRSLight>::iterator	vi;
	vi = find(lights.begin(), lights.end(), light);
	
	if (vi!=lights.end()) {
		lights.erase(vi);	
	}
}


//
//	ERScene::RemoveEntity
//
void ERScene::RemoveEntity( IPxRSEntity entity )
{
	if (!entity) {
		return;
	}
	
	vector<IPxRSEntity>::iterator	vi;
	vi = find(entities.begin(), entities.end(), entity);
	
	if (vi!=entities.end()) {
		entities.erase(vi);	
	}
}


//
//	ERScene::AddStaticMesh
//	extremely stupid solution...
//
void ERScene::AddStaticMesh( const IPxTriMesh mesh, const EVec4 &pos, const EQuat &orient )
{
	AddEntity(mesh, pos, orient);
}


//
//	ERScene::SetView
//
void ERScene::SetView( const EVec4 &position, const EQuat &orient )
{
	ASSERT( position.w!=0 );
	
	EMatrix4	T	=	Matrix4Translate( -position / position.w );
	EMatrix4	R	=	QuatToMatrix( QuatInverse( orient ) );

	view_matrix		=	T * R;
	view_position	=	position / position.w;
	
	light_rend->SetView( view_matrix, proj_matrix, position );
}


//
//	ERScene::SetProjection
//
void ERScene::SetProjection( float znear, float zfar, float width, float height )
{
	float w = znear * width  / zfar;
	float h = znear * height / zfar;
	proj_matrix	=	Matrix4PerspectiveRH(w, h, znear, zfar);

	light_rend->SetView( view_matrix, proj_matrix, view_position );
}


//
//	ERScene::SetAmbientLevel
//
void ERScene::SetAmbientLevel( const EVec4 &color )
{
	light_rend->SetAmbientLevel(color);
}


//
//	ERScene::AddGFXSprite
//
void ERScene::AddGFXSprite( const ERSGFXSprite_s &sprite )
{
	gfx->AddGFXSprite( sprite );
}


//
//	ERScene::AddGFXStrip
//
void ERScene::AddGFXStrip( const ERSGFXStrip_s &strip )
{
	gfx->AddGFXStrip( strip );
}


/*-----------------------------------------------------------------------------
	Scene rendering :
-----------------------------------------------------------------------------*/

//
//	ERScene::DebugTexture
//
void ERScene::DebugTexture( float x, float y, float w, float h, IPxTexture tex )
{
	//	TODO : DEBUG TEXTURES!
	//draw2d->Begin2D();
	//draw2d->Set2DViewMode();

	//txm->SetTexture( 0, tex );
	//
	//ERSQuad_s q;
	//q.p0	=	EVec2(x, y);
	//q.uv0	=	EVec2(0, 0);
	//q.p1	=	EVec2(x+w, y+h);
	//q.uv1	=	EVec2(1, 1);
	//q.color	=	EVec4(1,1,1,1);
	//
	//draw2d->DrawQuads(1, &q);
	//
	//draw2d->End2D();
}

//
//	ERScene::ReloadShaders
//
int ERScene::ReloadShaders( lua_State *L )
{
	ERScene	*self = (ERScene*)LuaGetSelf(L);
	
	self->sshs->ReloadShaders();
	self->light_rend->ReloadShaders();
	self->scr_fx->ReloadShaders();	
	self->gfx->ReloadShaders();

	self->draw2d.To<ED3DDraw2D>()->ReloadShaders();
	
	return 0;
}

